package bugfix.com.eventbustest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import bean.User;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;
import eventbus.Event;

public class MainActivity extends AppCompatActivity {

    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
        EventBus.getDefault().register(this);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, TestActivity.class);
                startActivity(intent);
            }
        });
//        Map<String, String> map = new HashMap<>();
//        map.put("aa", "bb");
//        map.put("aa1", "bb1");
//        map.put("aa2", "bb2");
//        map.put("aa3", "bb3");
//        ;
//        Log.v("----mapToString---", "mapToString------" + mapToString(map));
    }

    @Subscribe(threadMode = ThreadMode.MainThread) //在ui线程执行
    public void onStringEvent(String event) {
        Log.v("----onStringEvent---", "onStringEvent------" + event);
        tv.setText("发送回来的是String类型：====" + event);
    }

    @Subscribe(threadMode = ThreadMode.MainThread) //在ui线程执行
    public void onIntegerEvent(Integer event) {
        Log.v("----onIntegerEvent---", "onIntegerEvent------" + event);
        tv.setText("发送回来的是Integer类型：====" + event);
    }

    @Subscribe(threadMode = ThreadMode.MainThread) //在ui线程执行
    public void onUserEvent(User event) {
        Log.v("----onUserEvent---", "onUserEvent------" + event.toString());
        tv.setText("发送回来的是User类型：姓名：====" + event.getName() + "性别：====" + event.getSex());
    }

    @Subscribe(threadMode = ThreadMode.PostThread, priority = 80) //
    public void onUsersEvent(Event.UserListEvent events) {
        for (User u : events.users) {
            Log.v("----onUserEvent---", "onUserEvent------" + u.toString());
        }
        //截断下发
        EventBus.getDefault().cancelEventDelivery(events);
    }

    @Subscribe(threadMode = ThreadMode.PostThread, priority = 10) //
    public void onUsersEvent1(Event.UserListEvent events) {
        for (User u : events.users) {
            Log.v("----onUserEvent--1-", "onUserEvent----1--" + u.toString());
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

//    private String mapToString(Map<String, String> params) {
//        if (null == params || params.size() == 0) {
//            return "";
//        }
//        StringBuilder sb = new StringBuilder();
//        for (String str : params.keySet()) {
//            String value = params.get(str);
//            sb.append(str + "=" + value + "&");
//        }
//        return "?" + sb.toString().substring(0, sb.length() - 1);
//    }

}
